package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Category;
import com.beans.Movie;
import com.bussiness.MovieBussinessProxy;
import com.bussiness.PersonBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;

/**
 * Servlet implementation class GetCategoryServlet
 */
public class GetCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MovieBussinessProxy movieBI = new MovieBussinessProxy();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetCategoryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			HttpSession session = request.getSession(true);
			RequestDispatcher dispatcher;
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			
				int[] catids = movieBI.getAllCatIds();
				 Category[] catList = new Category[catids.length];
				for(int i=0;i<catids.length;i++){
					Category cat = new Category();
					cat.setCat_id(catids[i]);
					cat.setCat_name(movieBI.getCategoryName(catids[i]));
					catList[i]=cat;
				}
				session.setAttribute("catList", catList);
						
			
			dispatcher = getServletContext().getRequestDispatcher("/searchmovie.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(Exception e)
		{
			HttpSession session = request.getSession(true);
			session.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/searchmovie.jsp");
			dispatcher.forward(request, response);
		}
	}

	}


